#include "APP_KEY.h"
#include "gpio.h"


void APP_KEY_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	/* GPIO Ports Clock Enable */
	KEY_TACT_LIMIT_PORT_CLK();
	KEY_PUSH_KEY4_PORT_CLK();

	GPIO_InitStruct.Pin = KEY_TACT_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	HAL_GPIO_Init(KEY_TACT_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = KEY_LIMIT_PIN;
	HAL_GPIO_Init(KEY_LIMIT_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = KEY_PUSH_PIN;
	HAL_GPIO_Init(KEY_PUSH_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = KEY4_PIN;
	HAL_GPIO_Init(KEY4_PORT, &GPIO_InitStruct);
}

void APP_KEY_Scan(uint8_t *KEY_Scan)
{
//	static uint8_t KEY_TACT_press = 0;
//	static uint8_t KEY_LIMIT_press = 0;
	static uint8_t KEY_PUSH_press = 0;
	static uint8_t KEY4_press = 0;
	
	*KEY_Scan = 0;
	
//	if(KEY_TACT_READ() == KEY_PRESSED)
//	{
//		HAL_Delay(20);
//		if(KEY_TACT_READ() == KEY_PRESSED)
//		{
//			if(KEY_TACT_press == 0)
//			{
//				KEY_TACT_press = 1;
//				*KEY_Scan |= KEY_TACT;
//			}
//		}
//	}
//	else
//		KEY_TACT_press = 0;
	
	if(KEY_TACT_READ() == KEY_PRESSED)
	{
		HAL_Delay(20);
		if(KEY_TACT_READ() == KEY_PRESSED)
		{
			*KEY_Scan |= KEY_TACT;
		}
	}
	
	if(KEY_LIMIT_READ() == KEY_PRESSED)
	{
		HAL_Delay(20);
		if(KEY_LIMIT_READ() == KEY_PRESSED)
		{
			*KEY_Scan |= KEY_LIMIT;
		}
	}
//	if(KEY_LIMIT_READ() == KEY_PRESSED)
//	{
//		HAL_Delay(20);
//		if(KEY_LIMIT_READ() == KEY_PRESSED)
//		{
//			if(KEY_LIMIT_press == 0)
//			{
//				KEY_LIMIT_press = 1;
//				*KEY_Scan |= KEY_LIMIT;
//			}
//		}
//	}
//	else
//		KEY_LIMIT_press = 0;
	
	
//	if(KEY_PUSH_READ() == KEY_PRESSED)
//	{
//		HAL_Delay(20);
//		if(KEY_PUSH_READ() == KEY_PRESSED)
//		{
//			*KEY_Scan |= KEY_PUSH;
//		}
//	}
	if(KEY_PUSH_READ() == KEY_PRESSED)
	{
		HAL_Delay(20);
		if(KEY_PUSH_READ() == KEY_PRESSED)
		{
			if(KEY_PUSH_press == 0)
			{
				KEY_PUSH_press = 1;
				*KEY_Scan |= KEY_PUSH;
			}
		}
	}
	else
		KEY_PUSH_press = 0;
	
	if(KEY4_READ() == KEY_PRESSED)
	{
		HAL_Delay(20);
		if(KEY4_READ() == KEY_PRESSED)
		{
			if(KEY4_press == 0)
			{
				KEY4_press = 1;
				*KEY_Scan |= KEY4;
			}
		}
	}
	else
		KEY4_press = 0;
}




